package com.zj.leetcode.easy.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: algorithm
 * @description: 二叉树的所有路径
 * 257 binary-tree-paths
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class BinaryTreePaths {
    public static void main(String[] args) {
        Solution solution = new BinaryTreePaths().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        public List<String> binaryTreePaths(TreeNode root) {

            List<String> ret = new ArrayList<>();
            if (root == null) {
                return ret;
            }
            this.dfs(root, ret, "");
            return ret;
        }


        private void dfs(TreeNode node, List<String> list, String path) {
            path += node.val;

            if (node.left == null && node.right == null) {
                list.add(path);
                return;
            }

            if (node.left != null) {
                this.dfs(node.left, list, path + "->");
            }
            if (node.right != null) {
                this.dfs(node.right, list, path + "->");
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
